package com.codbking.calendar.exaple.richeditor;

import android.util.Log;
import android.widget.EditText;

abstract class AbstractTextStyle<T> {
    String TAG = "AbstractTextStyle";
    boolean isSelect = false;
    CursorPositionChangeListener cursorPositionChangeListener = new CursorPositionChangeListener() {
        @Override
        public void setCursorPositionChangeListener(int textStyle) {

        }
    };
    Class<T> spanType;
    int type;

    AbstractTextStyle(Class<T> spanType, int type) {
        this.type = type;
        this.spanType = spanType;
    }

    public void setCursorPositionChangeListener(CursorPositionChangeListener cursorPositionChangeListener) {
        this.cursorPositionChangeListener = cursorPositionChangeListener;
    }

    void changeSelect() {
        isSelect = !isSelect;
    }

    abstract void cursorPositionChanged(EditText currentEditText, int start, int digits);

    abstract void inputTextChanged(EditText currentEditText, int start, int count);

    void backspaceTextChanged(EditText currentEditText, int start, int before) {
        Log.d(TAG, "backspaceTextChanged " + currentEditText.getText() + " " + start + "  " + before);
        T[] typeSpans = currentEditText.getEditableText().getSpans(start - 1, start, this.spanType);
        for (int i = 0; i < typeSpans.length; i++) {
            int spanStart = currentEditText.getEditableText().getSpanStart(typeSpans[i]);
            int spanEnd = currentEditText.getEditableText().getSpanEnd(typeSpans[i]);
            Log.d(TAG, "backspaceTextChanged currentEditText.getSelectionStart() " + currentEditText.getSelectionStart()
                    + " spanStart " + spanStart + " spanEnd " + spanEnd + " start " + start + " before " + before);
            if (spanStart == spanEnd) {
                currentEditText.getEditableText().removeSpan(typeSpans[i]);
            }
        }
    }
}
